/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yipinsoft.modules.wechat.user.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.yipinsoft.modules.wechat.user.entity.WechatUser;
import com.yipinsoft.modules.wechat.user.service.WechatUserService;

/**
 * 微信用户记录系统中用户或会员的微信信息Controller
 * @author liulin
 * @version 2017-10-23
 */
@Controller
@RequestMapping(value = "${adminPath}/user/wechatUser")
public class WechatUserController extends BaseController {

	@Autowired
	private WechatUserService wechatUserService;
	
	@ModelAttribute
	public WechatUser get(@RequestParam(required=false) String id) {
		WechatUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = wechatUserService.get(id);
		}
		if (entity == null){
			entity = new WechatUser();
		}
		return entity;
	}
	
	@RequiresPermissions("user:wechatUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(WechatUser wechatUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WechatUser> page = wechatUserService.findPage(new Page<WechatUser>(request, response), wechatUser); 
		model.addAttribute("page", page);
		return "wechat/user/wechatUserList";
	}

	@RequiresPermissions("user:wechatUser:view")
	@RequestMapping(value = "form")
	public String form(WechatUser wechatUser, Model model) {
		model.addAttribute("wechatUser", wechatUser);
		return "wechat/user/wechatUserForm";
	}

	@RequiresPermissions("user:wechatUser:edit")
	@RequestMapping(value = "save")
	public String save(WechatUser wechatUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, wechatUser)){
			return form(wechatUser, model);
		}
		wechatUserService.save(wechatUser);
		addMessage(redirectAttributes, "保存微信用户成功");
		return "redirect:"+Global.getAdminPath()+"/user/wechatUser/?repage";
	}
	
	@RequiresPermissions("user:wechatUser:edit")
	@RequestMapping(value = "delete")
	public String delete(WechatUser wechatUser, RedirectAttributes redirectAttributes) {
		wechatUserService.delete(wechatUser);
		addMessage(redirectAttributes, "删除微信用户成功");
		return "redirect:"+Global.getAdminPath()+"/user/wechatUser/?repage";
	}

}